package com.facebook.fresco.samples.showcase.drawee;

import androidx.annotation.Nullable;
import com.facebook.drawee.drawable.ScalingUtils;
import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.fresco.samples.showcase.ResourceTable;
import com.facebook.fresco.samples.showcase.common.SimpleScaleTypeDialog;
import com.facebook.fresco.samples.showcase.misc.ImageUriProvider;
import com.oszc.bbhmlibrary.utils.LogUtil;
import com.oszc.bbhmlibrary.utils.MainHandler;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Text;
import ohos.agp.utils.Point;
import ohos.utils.net.Uri;

public class DraweeScaleTypeAbility extends Ability {
    private final String TAG = DraweeScaleTypeAbility.class.getSimpleName();
    private SimpleDraweeView mDraweeTop1;
    private SimpleDraweeView mDraweeTop2;
    private SimpleDraweeView mDraweeMain;
    private Text mSpinner;
    private int mSelectIdx= 0;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_layout_drawee_scale_type);
        findComponentById(ResourceTable.Id_drawee_view_top_1);

        final Uri uri1 =
                ImageUriProvider.createSampleUri(
                        ImageUriProvider.ImageSize.M,
                        ImageUriProvider.Orientation.LANDSCAPE,
                        null);
        final Uri uri2 =
                ImageUriProvider.createSampleUri(
                        ImageUriProvider.ImageSize.M,
                        ImageUriProvider.Orientation.PORTRAIT,
                        null);

        mDraweeTop1 = (SimpleDraweeView) findComponentById(ResourceTable.Id_drawee_view_top_1);
        mDraweeTop2 = (SimpleDraweeView) findComponentById(ResourceTable.Id_drawee_view_top_2);
        mDraweeMain = (SimpleDraweeView) findComponentById(ResourceTable.Id_drawee_view);
        mSpinner = (Text) findComponentById(ResourceTable.Id_fpinner);
        MainHandler.postDelay(()->{
            mDraweeTop1.setImageURI(uri1);
            mDraweeTop2.setImageURI(uri2);
            changeMainDraweeUri(uri1);
            },500);
        mDraweeTop1.setClickedListener(view-> changeMainDraweeUri(uri1));
        mDraweeTop2.setClickedListener(view-> changeMainDraweeUri(uri2));

        mSpinner.setClickedListener((view)->{
            SimpleScaleTypeDialog.createDialog(view.getContext(), mSelectIdx, (index, entry) -> {
                mSelectIdx = index;
                mSpinner.setText(entry.description);
                changeMainDraweeScaleType(entry.scaleType,entry.focusPoint);
            });
        });
    }

    private void changeMainDraweeUri(Uri uri) {
        mDraweeMain.setImageURI(uri);
    }
    private void changeMainDraweeScaleType(ScalingUtils.ScaleType scaleType, @Nullable Point focusPoint) {
        final GenericDraweeHierarchy hierarchy = mDraweeMain.getHierarchy();
        LogUtil.error(TAG,"changeMainDraweeScaleType A");
        hierarchy.setActualImageScaleType(scaleType);
        LogUtil.error(TAG,"changeMainDraweeScaleType B");
        hierarchy.setActualImageFocusPoint(focusPoint != null ? focusPoint : new Point(0.5f, 0.5f));
    }
}
